package map.practices;

import java.util.*;

//统计四个地方投票
public class Demo {
    public static void main(String[] args) {
        //创建数组放入每位学生投的结果
        List<String> locations = new ArrayList<>();
        Random r = new Random();
        //创建地区数组
        String[] names = {"西湖", "迪士尼", "天坛", "少林寺"};
        for (int i = 1; i <= 80; i++) {
            int index = r.nextInt(names.length);
            locations.add(names[index]);
        }
        //统计景点的次数
        Map<String, Integer> map = new HashMap<>();
        for (String location : locations) {
//            if (map.containsKey(location)) {
//                map.put(location, map.get(location) + 1);
//            } else {
//                map.put(location, 1);
//            }
            //默认值为0，匹配到后+1
            map.put(location, map.getOrDefault(location, 0) + 1);
        }
        map.forEach((k, v) -> System.out.println(k + "被选择了" + v+"次"));
    }
}

